package com.icim.csot.exporter.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.icim.core.service.IAlarmSenderService;
import com.icim.csot.exporter.job.CsotExportDefectInfoJob;
import com.icim.csot.exporter.job.CsotExportHistoryJob;
import com.icim.csot.exporter.job.CsotExportProcessParamJob;
import com.icim.csot.exporter.job.CsotExportSpcMfgDataJob;
import com.icim.twqc.exporter.job.TwqcExportProd_processJob;

@Controller
public class ExportDataContraller {
    @Resource
    CsotExportHistoryJob csotExportHistoryJob;
    @Resource
    CsotExportSpcMfgDataJob csotExportSpcMfgDataJob;
    @Resource
    CsotExportDefectInfoJob csotExportDefectInfoJob;
    @Resource
    CsotExportProcessParamJob csotExportProcessParamJob;
    @Resource
    TwqcExportProd_processJob twqcExportProd_processJob;
    @Resource
    private IAlarmSenderService alarmSenderService;

    @RequestMapping("reExport.do")
    public String exportShippingData(String beginTime, String endTimes, String type, String custom)throws Exception {

        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp beginTimestamp = new Timestamp(sd.parse(beginTime).getTime());
        Timestamp endTimestamp = new Timestamp(sd.parse(endTimes).getTime());
        if(custom.equals("007"))
        {
	        if (type.equals("HIS")) {
	            csotExportHistoryJob.setBeginTimestamp(beginTimestamp);
	            csotExportHistoryJob.setEndTimestamp(endTimestamp);
	            csotExportHistoryJob.setStrBeginTime(beginTime.replaceAll("-", "").replace(" ", "").replace(":", ""));
	            csotExportHistoryJob.setStrEndTime(endTimes.replaceAll("-", "").replace(" ", "").replace(":", ""));
	            try {
					csotExportHistoryJob.subMainProc2();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					String errorMsg="历史数据回传失败，开始时间："+beginTimestamp+"  结束时间："+endTimestamp+"\n"+"失败原因："+e.getMessage();
		        	alarmSenderService.sendAlarm1("007",errorMsg);
				}
	        } else if (type.equals("SPC")) {
	            csotExportSpcMfgDataJob.setBeginTimestamp(beginTimestamp);
	            csotExportSpcMfgDataJob.setEndTimestamp(endTimestamp);
	            // csotExportSpcMfgDataJob.setStrBeginTime(beginTime.replaceAll("-",
	            // "").replace(" ", "").replace(":", ""));
	            csotExportSpcMfgDataJob.setStrEndTime(endTimes.replaceAll("-", "").replace(" ", "").replace(":", ""));
	            try {
					csotExportSpcMfgDataJob.subMainProc2();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					String errorMsg="spc数据回传失败，开始时间："+beginTimestamp+"  结束时间："+endTimestamp+"\n"+"失败原因："+e.getMessage();
		        	alarmSenderService.sendAlarm1("007",errorMsg);
				}
	        } else if (type.equals("DEF")) {
	            csotExportDefectInfoJob.setBeginTimestamp(beginTimestamp);
	            csotExportDefectInfoJob.setEndTimestamp(endTimestamp);
	            // csotExportDefectInfoJob.setStrBeginTime(beginTime.replaceAll("-",
	            // "").replace(" ", "").replace(":", ""));
	            csotExportDefectInfoJob.setStrEndTime(endTimes.replaceAll("-", "").replace(" ", "").replace(":", ""));
	            try {
					csotExportDefectInfoJob.subMainProc2();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					String errorMsg="defect数据回传失败，开始时间："+beginTimestamp+"  结束时间："+endTimestamp+"\n"+"失败原因："+e.getMessage();
		        	alarmSenderService.sendAlarm1("007",errorMsg);
				}
	        } else if (type.equals("PRO")) {
	            csotExportProcessParamJob.setBeginTimestamp(beginTimestamp);
	            csotExportProcessParamJob.setEndTimestamp(endTimestamp);
	            csotExportProcessParamJob.setStrBeginTime(beginTime.replaceAll("-", "").replace(" ", "").replace(":", ""));
	            csotExportProcessParamJob.setStrEndTime(endTimes.replaceAll("-", "").replace(" ", "").replace(":", ""));
	            // csotExportDefectInfoJob.setStrBeginTime(beginTime.replaceAll("-",
	            // "").replace(" ", "").replace(":", ""));
	            // csotExportDefectInfoJob.setStrEndTime(endTimes.replaceAll("-",
	            // "").replace(" ", "").replace(":", ""));
	            try {
					csotExportProcessParamJob.subMainProc2();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					String errorMsg="process数据回传失败，开始时间："+beginTimestamp+"  结束时间："+endTimestamp+"\n"+"失败原因："+e.getMessage();
		        	alarmSenderService.sendAlarm1("007",errorMsg);
				}
	        }
        }
        else if(custom.equals("232"))
        {
        	if (type.equals("HIS")) {
        		twqcExportProd_processJob.setBeginTimestamp(beginTimestamp);
        		twqcExportProd_processJob.setEndTimestamp(endTimestamp);
        		twqcExportProd_processJob.setStrBeginTime(beginTime.replaceAll("-", "").replace(" ", "").replace(":", ""));
        		twqcExportProd_processJob.setStrEndTime(endTimes.replaceAll("-", "").replace(" ", "").replace(":", ""));
                try {
                	twqcExportProd_processJob.subMainProc2();
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				String errorMsg="232客户历史数据回传失败，开始时间："+beginTimestamp+"  结束时间："+endTimestamp+"\n"+"失败原因："+e.getMessage();
    	        	alarmSenderService.sendAlarm1("232",errorMsg);
    			}
            }
        }
        return "result";
    }
}
